package com.smscenter.server.core.tradingcenter;

import com.google.common.collect.Maps;
import com.smscenter.server.core.IExecutor;
import com.zeus.base.privilege.exception.ConcurrentException;
import com.zeus.base.privilege.model.TbCompany;
import com.zeus.base.privilege.model.TbUser;
import com.zeus.base.privilege.service.IUserService;
import com.zeus.base.privilege.utils.ArithUtil;
import com.zeus.base.privilege.utils.DateUtil;
import com.zeus.base.privilege.utils.ResultModel;
import com.zeus.base.privilege.utils.StringUtil;
import com.zeus.base.smscenter.common.constant.SmsConstants;
import com.zeus.base.smscenter.common.constant.TradingCenterConstants;
import com.zeus.base.smscenter.common.security.MD5;
import com.zeus.base.smscenter.common.util.WebUtils;
import com.zeus.base.tradingcenter.model.ConsumeBalance;
import com.zeus.base.tradingcenter.model.ConsumeDetail;
import com.zeus.base.tradingcenter.model.ConsumeType;
import com.zeus.base.tradingcenter.service.IConsumeBalanceService;
import com.zeus.base.tradingcenter.service.IConsumeDetailService;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易中心短信扣费
 * Created by liwenhua on 2017-10-25.
 */
public class TradingConsumExecutor {

	public static void main(String[] args) throws IOException {
		
		TradingConsumExecutor w = new TradingConsumExecutor();
		Map map = new HashMap();
        map.put("userName", "root");
        map.put("password", MD5.generate("root",false).toLowerCase());
        map.put("type", "短信");
        map.put("money", "0.2");
        map.put("companyCode", "Fy56");
        map.put("remark", "CESHI消费");
//		System.out.println(w.excute(map));
	}

    /**
     * 短信消费扣费
     * @param consumeType
     * @param amount
     * @param user
     * @param iConsumeDetailService
     * @return
     */
    public ResultModel excuteConsume(ConsumeType consumeType, Double amount, TbUser user, IConsumeDetailService iConsumeDetailService) {
        if (amount == null || amount <= 0) {
            return new ResultModel(false, "消费金额必须大于0!");
        }
        if (consumeType == null || consumeType.getConsumeTypeId() == null) {
            return new ResultModel(false, "交易中心消费项目不存在!");
        }
        if (user == null || user.getUserId() == null) {
            return new ResultModel(false, "认证中心用户信息不正确!");
        }
        TbCompany company = user.getCompany();
        if (company == null || company.getCompanyId() == null) {
            return new ResultModel(false, "认证中心用户公司信息不正确!");
        }
        ConsumeDetail consumeDetail = new ConsumeDetail();
        consumeDetail.setConsumeType(consumeType);
        consumeDetail.setAmounts(amount);
        consumeDetail.setSources(0);//现金
        consumeDetail.setRemark("发送短信扣费");
        consumeDetail.setParam(true, false, user);
        consumeDetail.setCompanyId(company.getCompanyId());
        consumeDetail.setCompanyCode(company.getCompanyCode());
        consumeDetail.setCompanyName(company.getCompanyName());
        boolean concurrent;
        ResultModel r = new ResultModel(false);
        do {
            try {
                r = iConsumeDetailService.consume(consumeDetail);
                concurrent = false;
            } catch (ConcurrentException e) {
                r.setSuccess(e.getResultModel().isSuccess());
                r.setCode(e.getResultModel().getCode());
                r.setMessage(e.getResultModel().getMessage());
                concurrent = r.getCode() == 0;//并发
            }
        } while (concurrent);
        return r;
    }

    /**
     * 账户回冲
     * @param tradNo  交易单号
     * @param user
     * @param iConsumeDetailService
     * @return
     */
    public ResultModel excuteRevertConsume(String tradNo, TbUser user, String remark, IConsumeDetailService iConsumeDetailService) {
        if (StringUtil.isEmpty(tradNo)) {
            return new ResultModel(false, "消费金额必须大于0!");
        }
        boolean concurrent;
        ResultModel r = new ResultModel(false);
        do {
            ConsumeDetail consumeDetail = new ConsumeDetail();
            try {
                //账目系统自动回冲(交易失败时,做账目的回冲.就是说提前预扣的费用,再以系统自动充值的名义补充回去.)
                consumeDetail.setConsumeDetailNo(tradNo);
                consumeDetail.setParam(true, false, user);
                consumeDetail.setRemark(remark);
                r = iConsumeDetailService.consumeRevert(consumeDetail);
                concurrent = false;
            } catch (ConcurrentException e) {
                r.setSuccess(e.getResultModel().isSuccess());
                r.setCode(e.getResultModel().getCode());
                r.setMessage(e.getResultModel().getMessage());
                concurrent = r.getCode() == 0;//并发
            }
        } while (concurrent);
        return r;
    }

    /**
     * 查询交易中心短信余额
     * @param companyId 公司ID
     * @param consumeTypeId 消费项目ID
     * @return  查出多个返回null
     */
    public ConsumeBalance getConsumeBalance(Integer companyId, Integer consumeTypeId, IConsumeBalanceService iConsumeBalanceService) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("companyId" , companyId);
        map.put("consumeTypeId" , consumeTypeId);
        ConsumeBalance consumeBalance = null;
        consumeBalance = iConsumeBalanceService.selectByParams(map);
        if (consumeBalance == null) {
            consumeBalance = new ConsumeBalance();
            consumeBalance.setBalance(0d);
        }
        return consumeBalance;
    }


}
